from src.core.item_manager import create_item, delete_item
from src.ui.console.display import display_items
from src.ui.console.menu import display_menu
from src.storage.file_manager import save_to_file, load_from_file

def main():
    items = []
    if not isinstance(items, list):
        items = []
    items = load_from_file("chuer-zoo-lilian/data/pets.json") 
    print("🎉 Welcome to Electronic Pet collection Management System")
    while True:
        display_menu()
        choice = input("choose (1-6): ")
        if choice == "1":  
            name = input("input pet name:")
            item_type = input("which type(form Warrior,Assassin,Mage,Marksman,Support): ")
            while True:
                age_input = input("input its age: ")
                if age_input.isdigit():
                    age = int(age_input)
                    break
                print("❌ must be number")
            success, message = create_item(items, name, item_type, age)
            print(message)
        
        elif choice == "2": 
            display_items(items)
        
        elif choice == "3": 
            user_input_id = input("input the ID of who u want to delete: ")
            try:
                item_id = int(user_input_id)
                delete_item(items, item_id)
            except ValueError:
                print("Error: ID must be an integer")
        
        elif choice == "4": 
            save_to_file(items)
        
        elif choice == "5": 
            items = load_from_file()
            print("✅ Loaded from file successfully")
        
        elif choice == "6": 
            save_to_file(items) 
            print("👋 thank you for using")
            break
        
        else:
            print("❌ Please input number 1-6")

if __name__ == "__main__":
    main()
